package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.GoodsTypeMapper;
import com.zld.smarket.dao.SupplierMapper;
import com.zld.smarket.pojo.Goods;
import com.zld.smarket.pojo.GoodsType;
import com.zld.smarket.pojo.Supplier;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Supplier> suppliers = supplierMapper.selectAllAndPage(page,limit);
        Long count = supplierMapper.countSupplier();
        return new ResponseData(suppliers,count);
    }

    @Override
    public Integer deleteById(Long[] ids) {
        int j=0;
        try {
            //先判定数据是否存在
            for (Long id : ids) {
                Supplier supplier = supplierMapper.selectByPrimaryKey(id);
                if(supplier != null) {
                    j = supplierMapper.deleteByPrimaryKey(id);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
//            设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return j;
    }

    @Override
    public ResponseData search(String s, Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Supplier> suppliers =  supplierMapper.search(s,page,limit);
        Long count = Long.valueOf(suppliers.size());
        return new ResponseData(suppliers,count);
    }

    @Override
    public Integer addSupplier(Supplier supplier) {
        int j=0;
        j=supplierMapper.insert(supplier);
        return j;
    }

    @Override
    public ResponseData updateById(Supplier supplier) {
        try {
            Supplier supplier1 = supplierMapper.selectByPrimaryKey(supplier.getSupplierId());
            //还需要加逻辑判定  -- 去学习正则表达式
            if(supplier1!=null){
                int i =  supplierMapper.updateByPrimaryKey(supplier);
                if(i==0){
                    return new ResponseData(ExceptionMsg.FAILD);
                }else{
                    return new ResponseData(ExceptionMsg.SUCCESS);
                }
            }else {
                return new ResponseData("000407","请刷新以后在尝试！");
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

    @Override
    public List<Supplier> selectAll() {
        return supplierMapper.selectAll();
    }
}
